/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entities.OrderEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import sessionBeans.OrderEntityFacade;

/**
 *
 * @author HOANG ANH
 */
@ManagedBean
@RequestScoped
public class OrderBean {

    @EJB
    private OrderEntityFacade orderEntityFacade;
    @Resource(name = "jms/OrderMessage")
    private Queue orderMessage;
    @Resource(name = "jms/OrderMessageFactory")
    private ConnectionFactory orderMessageFactory;
    private List<OrderEntity> orderList;
    private String customer, product;
    private Date requiredDate;

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public Date getRequiredDate() {
        return requiredDate;
    }

    public void setRequiredDate(Date requiredDate) {
        this.requiredDate = requiredDate;
    }

    public List<OrderEntity> getOrderList() {
        if (orderList==null) {
            orderList = new ArrayList<OrderEntity>();
        }
        if (orderList.isEmpty()) {
            orderList = orderEntityFacade.findAll();
        }
        return orderList;
    }

    public void setOrderList(ArrayList<OrderEntity> orderList) {
        this.orderList = orderList;
    }

    public String save() {
        try {
            OrderEntity ord = new OrderEntity();
            ord.setCustomer(customer);
            ord.setProduct(product);
            ord.setRequiredDate(requiredDate);
            sendJMSMessageToOrderMessage(ord);

            customer = "";
            product = "";
            requiredDate = null;
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,"Completed","Submit Finished");
            FacesContext.getCurrentInstance().addMessage(null, message);

            return null;
        } catch (JMSException ex) {
            ex.printStackTrace();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "JMS Exception", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, message);
            return null;
        }
    }

    /** Creates a new instance of OrderBean */
    public OrderBean() {
        
    }

    private Message createJMSMessageForjmsOrderMessage(Session session, OrderEntity order) throws JMSException {
        // TODO create and populate message to send
        ObjectMessage om = session.createObjectMessage();
        om.setObject(order);
        return om;
    }

    private void sendJMSMessageToOrderMessage(OrderEntity order) throws JMSException {
        Connection connection = null;
        Session session = null;
        MessageProducer messageProducer = null;
        try {
            connection = orderMessageFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = session.createProducer(orderMessage);
            messageProducer.send(createJMSMessageForjmsOrderMessage(session, order));
        } finally {
            if (messageProducer != null) {
                messageProducer.close();
            }
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
